#include <stdio.h>

void reverse(int *a, int n)
{
	int j,tmp;
	for(j = 0; j < n/2; j++){
		tmp = a[j];
		a[j] = a[n-1-j];
		a[n-1-j] = tmp;
	}
}

void permutations(int *a, int n)
{
	int i, h, j;
	int tmp;
	do{
		for(j = 0; j < n; j++)
			printf("%d,",a[j]);
		printf("\n");

		i = h = -1;
		/* find i */
		for(j = 1; j < n; j++)
			if(a[j-1] < a[j])
				i = j > i ? j : i;

		if( i == -1) return; /* just over here */
		
		/* find h */
		for(j = i; j < n; j++)
			if(a[i-1] < a[j])
				h = j > h ? j : h;

		tmp = a[i-1];
		a[i-1] = a[h];
		a[h] = tmp;
		
		reverse(&a[i], n  - i);	
		
	}while(1);
}
